package com.sdyd.antivirus.controller;

import com.alibaba.fastjson.JSONObject;
import com.sdyd.antivirus.model.CommunityInfo;
import com.sdyd.antivirus.service.CommunityInfoService;
import com.sdyd.antivirus.service.UserInfoService;
import com.sdyd.antivirus.utils.AesEncryptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("community")
public class CommunityInfoController {
    Logger logger= LoggerFactory.getLogger(CommunityInfoController.class);

    @Autowired
    private CommunityInfoService communityInfoService;

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("health")
    public String insertCommunityInfo(CommunityInfo communityInfo){
        JSONObject obj = new JSONObject();
        obj.put("success",true);
        try {
            //首先验证groupId是否合法
            String groupId = communityInfo.getGroupId();
            Map groupInfo = userInfoService.getGroupInfo(groupId);
            Float heatingDegree = communityInfo.getHeatingDegree();

            String phone = communityInfo.getPhoneNumber();
            AesEncryptUtil.decrypt(phone);
            String cardNumber = communityInfo.getCardNumber();
            AesEncryptUtil.decrypt(cardNumber);

            if(groupInfo == null){
                logger.error("CommunityInfoController插入用户信息失败：组织id不合法");
                obj.put("success",false);
            }else if(heatingDegree != null && (heatingDegree.floatValue()<35.0 || heatingDegree.floatValue()>42.0) ){
                logger.error("CommunityInfoController插入用户信息失败：体温范围错误");
                obj.put("success",false);
            }else {
                communityInfoService.insertCommunityInfo(communityInfo);
            }

        } catch (Exception e) {
            //e.printStackTrace();
            logger.error("CommunityInfoController插入用户信息失败："+e.getMessage());
            obj.put("success",false);
        }
        return obj.toString();
    }

}
